import week.command as wCommand

import month.command as mCommand

import pandas as pd
import util
import util.mydecorator
import util.lib as lib


week_data = None
weekIndex = []

month_data = None
monthIndex = []

def saveWeekData(sta_writer):
    global week_data, weekIndex, month_data, monthIndex

    # 周
    #up75dsData = util.multiCallBack(week_data,weekIndex[-52:],wCommand.up75dsCommand)
    #up75dsData.to_excel(sta_writer, sheet_name='突破75日线')

    #down75dsData = util.multiCallBack(week_data,weekIndex[-52:],wCommand.down75dsCommand)
    #down75dsData.to_excel(sta_writer, sheet_name='跌破75日线')

    # 周
    #up300dsData = util.multiCallBack(week_data,weekIndex[-104:],wCommand.up300dsCommand)
    #up300dsData.to_excel(sta_writer, sheet_name='突破300日线')

    #down300dsData = util.multiCallBack(week_data,weekIndex[-104:],wCommand.down300dsCommand)
    #down300dsData.to_excel(sta_writer, sheet_name='跌破300日线')

    #up300dPerformData = wCommand.up300dPerformCommand(week_data)
    #up300dPerformData.to_excel(sta_writer, sheet_name='300日线表现统计')

    #weekPctSta = util.multiCallBack(week_data,weekIndex[-26:],wCommand.weekStatisticCommand)
    # 不保留索引
    #weekPctSta.to_excel(sta_writer, sheet_name='周涨跌统计', index=False)

    #weekPBSta = util.multiCallBack(week_data,weekIndex[-52:],wCommand.weekPbRankStatisticCommand)
    #weekPBSta.to_excel(sta_writer, sheet_name='周PB_RANK统计')

    weekUp10lpData = util.multiCallBack(week_data,weekIndex[-72:],wCommand.up10lpCommand)
    weekUp10lpData.to_excel(sta_writer, sheet_name='周线突破10分位点', index=False)

    weekUp100lpData = util.multiCallBack(week_data,weekIndex[-72:],wCommand.up100lpCommand)
    weekUp100lpData.to_excel(sta_writer, sheet_name='周线突破100分位点', index=False)

    disStaticData = wCommand.disStaticCommand(week_data)
    disStaticData.to_excel(sta_writer, sheet_name='年线统计')

    lpStaticData = wCommand.lpStaticCommand(week_data)
    lpStaticData.to_excel(sta_writer, sheet_name='周分位点统计')

    #nsmalletsData = wCommand.nsmallestCommand(week_data)
    #nsmalletsData.to_excel(sta_writer, sheet_name='周下跌统计')


def saveMonthData(sta_writer):

    global week_data, weekIndex, month_data, monthIndex
    month_count = 36 

    # 月
    up10lpData = util.multiCallBack(month_data, monthIndex[-month_count:],mCommand.up10lpCommand)
    up10lpData.to_excel(sta_writer, sheet_name='突破10分位点', index=False)
    
    up100lpData = util.multiCallBack(month_data,monthIndex[-month_count:],mCommand.up100lpCommand)
    up100lpData.to_excel(sta_writer, sheet_name='月突破100分位点', index=False)

    down10lpData = util.multiCallBack(month_data, monthIndex[-month_count:],mCommand.down10lpCommand)
    down10lpData.to_excel(sta_writer, sheet_name='跌破10分位点', index=False)

    #monthPctSta = util.multiCallBack(month_data, monthIndex[-month_count:],mCommand.monthStatisticCommand)
    #monthPctSta.to_excel(sta_writer, sheet_name='月涨跌统计', index=False)

    lpDecreaseData = mCommand.lpDecreaseCommand(month_data)
    lpDecreaseData.to_excel(sta_writer, sheet_name='低分位点跌幅',index=False)

    lpStaticData = mCommand.lpStaticCommand(month_data)
    lpStaticData.to_excel(sta_writer, sheet_name='月分位点统计')

    gt1200dStaticData = mCommand.gt1200dStaticCommand(month_data)
    gt1200dStaticData.to_excel(sta_writer, sheet_name='1200日线统计')

    #pctStaticData = mCommand.pctStaticCommand(month_data)
    #pctStaticData.to_excel(sta_writer, sheet_name='月表现统计')

    #dividendYieldData = mCommand.dividendYieldCommand(month_data)
    #dividendYieldData.to_excel(sta_writer, sheet_name='股息率')


def read_data():
    global week_data, weekIndex, month_data, monthIndex

    ## 获取周数据
    week_data = lib.lib_get_week_data()

    ## 周的索引
    weekIndex = []
    for k,v in week_data.items():
        if len(v) > 200:
            weekIndex = v.index.values
            break


    # 月数据
    month_data = lib.lib_get_month_data()

    # 月索引
    monthIndex = []
    for k,v in month_data.items():
        if len(v) > 100:
            monthIndex = v.index.values
            break


@util.mydecorator.calTime
def job():
    read_data()

    # 保存至 excel
    sta_writer = pd.ExcelWriter(lib.data_path +'stock_sta.xlsx')

    saveWeekData(sta_writer)
    saveMonthData(sta_writer)

    sta_writer.close()


if __name__ == '__main__':
    job()